package cn.ainit.sys.ds.cloumns;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.PreparedStatement;

@Transactional
@Repository("queryJsonDao")
public class QueryJsonDaoImpl implements QueryJsonDao {
	
	private SessionFactory sessionFactory;
	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public String querySql(String id) throws Exception{
		try {
			String hql = "select sqlText from sysDataStore where dsId =?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, id);
			List<Object> objects = query.list();
			String sql="";
			if (objects != null && objects.size() > 0 ) {
				sql = (String)objects.get(0);
			}
			return sql;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List findObjectList(String sql) throws Exception {
		try {
			List<Object> list = new ArrayList<Object>();
			Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
			list = query.list();
			for (int i = 0; i <list.size(); i++) {
				System.out.println(list.get(i));
			}
			if (list!= null & list.size()>0) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List findObjectColumns(String id) throws Exception {
		try {
			List<sysCloumns> list = new ArrayList<sysCloumns>();
			String hql= "from sysCloumns s where s.dsId =?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, id);
			list = query.list();
			if (list != null & list.size()>0) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
